<template>
  <view class="wh_750 pageHeight bg_F5F5F5 flex flex-column align-center">
    <s-header-title isDisplayBack title="设置头像" is-fixed background="#FFFFFF"
                    @getPageHeight="getPageHeight"></s-header-title>
    <qf-image-cropper ref="qrImageCropper" :src="cropSrc" :width="page.width" :height="page.height"
                      :navigation="false"
                      @crop="cropSuccess"></qf-image-cropper>
  </view>
</template>

<script setup>
  import SHeaderTitle from '@/sheep/components/s-header-title/s-header-title.vue';
  import { computed, reactive } from 'vue';
  import QfImageCropper from '@/uni_modules/qf-image-cropper/components/qf-image-cropper/qf-image-cropper.vue';
  import { baseUrl } from '@/sheep/config';
  import sheep from '@/sheep';
  import { onLoad } from '@dcloudio/uni-app';

  const page = reactive({
    options: {},
    headerHeight: 0,
    width: uni.upx2px(250),
    height: uni.upx2px(350),
  });

  const cropSrc = computed(() => uni.getStorageSync('ancestralHallSrc'));

  onLoad((options) => {
    page.options = options;
    if (page.options.flag == 1) {
      page.width = uni.upx2px(350);
    }
  });

  function getPageHeight(number) {
    page.headerHeight = number;
  }

  function cropSuccess(e) {
    // #ifdef H5
    sheep.$platform.upload.uploadFile(e.tempFilePath, -1, async (data) => {
      if (page.options.flag == 1) {
        console.log(page.options);
        if (page.options.is_genealoy != 1) {
          let { code, msg } = await sheep.$api.family.updateFamilyMemberInfo({
            member_id: page.options.id,
            avatar: data.fullurl,
          });
          if (code == 1) {
            sheep.$helper.toast('修改成功');
            setTimeout(() => {
              uni.$emit('signature');
              uni.removeStorageSync('ancestralHallSrc');
              uni.navigateBack({
                delta: 2,
              });
            }, 1000);
          } else {
            sheep.$helper.toast(msg);
          }
        } else {
          let { code, msg } = await sheep.$api.genealogy.updateMemberInfo({
            id: page.options.id,
            head: data.fullurl,
          });
          if (code == 1) {
            sheep.$helper.toast('修改成功');
            setTimeout(() => {
              uni.removeStorageSync('ancestralHallSrc');
              uni.navigateBack({
                delta: 2,
              });
            }, 1000);
          } else {
            sheep.$helper.toast(msg);
          }
        }
      } else {
        let { code, msg } = await sheep.$api.genealogy.updateAncestralInfo({
          id: page.options.id,
          image: data.fullurl,
        });
        if (code == 1) {
          sheep.$helper.toast('修改成功');
          setTimeout(() => {
            uni.$emit('SET_AVATAR', data.fullurl);
            uni.removeStorageSync('ancestralHallSrc');
            uni.navigateBack();
          }, 1000);
        } else {
          sheep.$helper.toast(msg);
        }
      }
    });
    // #endif
    // #ifndef H5
    uni.uploadFile({
      url: `${baseUrl}/api/common/uploadFile`,
      filePath: e.tempFilePath,
      fileType: 'image',
      name: 'pics',
      formData: {
        'file': 'pics',
      },
      header: {
        // #ifdef MP
        'Content-Type': 'multipart/form-data',
        // #endif
        ['token']: uni.getStorageSync('token'),
        'file': e.tempFilePath,
      },
      success: async (res) => {
        let { data } = res.data ? JSON.parse(res.data) : {};
        if (page.options.flag == 1) {

          console.log(page.options);
          if (page.options.is_genealoy != 1) {
            let { code, msg } = await sheep.$api.family.updateFamilyMemberInfo({
              member_id: page.options.id,
              avatar: data.fullurl,
            });
            if (code == 1) {
              sheep.$helper.toast('修改成功');
              setTimeout(() => {
                uni.$emit('signature');
                uni.removeStorageSync('ancestralHallSrc');
                uni.navigateBack({
                  delta: 2,
                });
              }, 1000);
            } else {
              sheep.$helper.toast(msg);
            }
          } else {
            let { code, msg } = await sheep.$api.genealogy.updateMemberInfo({
              id: page.options.id,
              head: data.fullurl,
            });
            if (code == 1) {
              sheep.$helper.toast('修改成功');
              setTimeout(() => {
                uni.removeStorageSync('ancestralHallSrc');
                uni.navigateBack({
                  delta: 2,
                });
              }, 1000);
            } else {
              sheep.$helper.toast(msg);
            }
          }
        } else {
          let { code, msg } = await sheep.$api.genealogy.updateAncestralInfo({
            id: page.options.id,
            image: data.fullurl,
          });
          if (code == 1) {
            sheep.$helper.toast('修改成功');
            setTimeout(() => {
              uni.$emit('SET_AVATAR', data.fullurl);
              uni.removeStorageSync('ancestralHallSrc');
              uni.navigateBack();
            }, 1000);
          } else {
            sheep.$helper.toast(msg);
          }
        }
      },
    });
    // #endif
  }
</script>

<style lang="scss">

</style>
